package command;

/**
 * Created by lenovo on 2018/1/2.
 */
public class RemoteControlWithUndo {
    Command[] onCommands;
    Command[] offCommands;
    Command undoCommand;

    public RemoteControlWithUndo() {
        onCommands=new Command[7];
        offCommands=new Command[7];

        Command noCommand=new NoCommand();
        for(int i=0;i<7;i++){
            onCommands[i]=noCommand;
            offCommands[i]=noCommand;
        }
        undoCommand=noCommand;

    }
    public void setCommand(int solt,Command onCommand,Command offCommand){
        onCommands[solt]=onCommand;
        offCommands[solt]=offCommand;

    }
    public void onButtonWasPushed(int slot){
        onCommands[slot].execute();
        undoCommand=onCommands[slot];
    }

    public void offButtonWasPushed(int slot){

        offCommands[slot].execute();
        undoCommand=offCommands[slot];
    }

    public void undoButtonWasPushed(){
        undoCommand.undo();
    }

    @Override
    public String toString() {
        StringBuffer sb=new StringBuffer();
        sb.append("\n----------------RemoteControl--------------\n");
        for(int i=0;i<onCommands.length;i++){
            sb.append("[slot "+i+"]"+onCommands[i].getClass().getName()+" "+offCommands.getClass().getName()+"\n");
        }
        return sb.toString();
    }
}
